package com.prs.crm.action.oa;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.WorkSummary;
import com.prs.crm.domain.oa.WorkSummaryAudit;
import com.prs.crm.domain.oa.WorkSummaryCritique;
import com.prs.crm.domain.oa.WorkSummaryType;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.oa.NoticeService;
import com.prs.crm.service.oa.WorkSummaryAuditService;
import com.prs.crm.service.oa.WorkSummaryCritiqueService;
import com.prs.crm.service.oa.WorkSummaryService;
import com.prs.crm.service.oa.WorkSummaryTypeService;

public class WorkSummaryAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private WorkSummary workSummary;
	private WorkSummaryService workSummaryService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private WorkSummaryTypeService workSummaryTypeService;

	private WorkSummaryAudit workSummaryAudit;
	private WorkSummaryAuditService workSummaryAuditService;
	private WorkSummaryCritique workSummaryCritique;
	private WorkSummaryCritiqueService workSummaryCritiqueService;

	private List<WorkSummaryType> summaryTypes;
	private NoticeService noticeService;

	private String ydmc;

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	private File upload;
	private String uploadFileName;

	private String copyToPersons;

	@Action("listWorkSummary")
	public String execute() {
		return SUCCESS;
	}

	@Action("listWorkSummaryByMonth")
	public String listWorkSummaryByMonth() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				WorkSummary.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Date start = null;
		Date end = null;
		if (ydmc != null) {
			if (!"all".equals(ydmc)) {
				String t = ydmc;
				ydmc = ydmc.split(",")[0];
				if (ToolKits.isInteger(ydmc)) {
					criteria.createAlias("referPerson", "re");
					criteria.add(Restrictions.eq("re.id", Integer
							.parseInt(ydmc)));
				} else {
					if (ydmc.indexOf("$") != -1) {
						start = ReportUtils.convertToDate(ydmc.replace("$", "")
								.trim());
						end = ReportUtils.convertToDate(String.valueOf(Integer
								.parseInt(ydmc.replace("$", "").trim()) + 1));
					} else if (ydmc.indexOf("dept") != -1) {
						Collection<Integer> pes = new ArrayList<Integer>();
						for (String pe : t.split(",")) {
							if (pe.indexOf("dept") == -1) {
								pes.add(Integer.parseInt(pe));
							}
						}
						if (pes.size() > 0) {
							criteria.createAlias("referPerson", "re");
							criteria.add(Restrictions.in("re.id", pes));
						}
					} else if (ydmc.indexOf("-") != -1) {
						start = ReportUtils.convertToDateFrom(ydmc);
						Calendar cal = Calendar.getInstance();
						cal.setTime(start);
						String s = String
								.valueOf(cal.get(Calendar.MONTH) + 2 > 12 ? "12"
										: "" + (cal.get(Calendar.MONTH) + 2));
						if (cal.get(Calendar.MONTH) + 2 > 12)
							end = ReportUtils.convertToDate(String.valueOf(cal
									.get(Calendar.YEAR)), s, "31");
						else
							end = ReportUtils.convertToDate(String.valueOf(cal
									.get(Calendar.YEAR)), s);
					} else {
						ydmc = ReportUtils.URLDecode(ydmc.trim());
						criteria.createAlias("type", "ty");
						criteria.add(Restrictions.eq("ty.name", ydmc));
					}
				}
			}
		}
		if (start != null && end != null) {
			criteria.add(Restrictions.ge("recordTime", start));
			criteria.add(Restrictions.lt("recordTime", end));
		}
		this.setPagination(this.getWorkSummaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listAuditWorkSummary")
	public String auditWorkSummary() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				WorkSummary.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Property.forName("auditPerson").eq(person));
		criteria.add(Property.forName("state").eq(Constrains.TASK_AUDITING));
		this.setPagination(this.getWorkSummaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listCritiqueWorkSummary")
	public String listCritiqueWorkSummary() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				WorkSummary.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("state", Constrains.TASK_AUDITING));
		criteria.createAlias("copyToPersons", "c").add(
				Restrictions.eq("c.id", person.getId()));
		this.setPagination(this.getWorkSummaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editAuditWorkSummary")
	public String editAuditWorkSummary() {
		prepCopyToPersons();
		return SUCCESS;
	}

	@Action("listMyWorkSummary")
	public String myWorkSummary() {
		return SUCCESS;
	}

	@Action("listMyWorkSummaryByMonth")
	public String listMyWorkSummaryByMonth() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				WorkSummary.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Property.forName("referPerson").eq(person));
		Date start = null;
		Date end = null;
		if (ydmc != null) {
			if (!"all".equals(ydmc)) {
				ydmc = ydmc.split(",")[0];
				if (ydmc.indexOf("$") != -1) {
					start = ReportUtils.convertToDate(ydmc.replace("$", "")
							.trim());
					end = ReportUtils.convertToDate(String.valueOf(Integer
							.parseInt(ydmc.replace("$", "").trim()) + 1));
				} else if (ydmc.indexOf("-") != -1) {
					start = ReportUtils.convertToDateFrom(ydmc);
					Calendar cal = Calendar.getInstance();
					cal.setTime(start);
					String s = String
							.valueOf(cal.get(Calendar.MONTH) + 2 > 12 ? "12"
									: "" + (cal.get(Calendar.MONTH) + 2));
					if (cal.get(Calendar.MONTH) + 2 > 12)
						end = ReportUtils.convertToDate(String.valueOf(cal
								.get(Calendar.YEAR)), s, "31");
					else
						end = ReportUtils.convertToDate(String.valueOf(cal
								.get(Calendar.YEAR)), s);
				} else {
					ydmc = ReportUtils.URLDecode(ydmc.trim());
					criteria.createAlias("type", "ty");
					criteria.add(Restrictions.eq("ty.name", ydmc));
				}
			}
		}
		if (start != null && end != null) {
			criteria.add(Restrictions.ge("recordTime", start));
			criteria.add(Restrictions.lt("recordTime", end));
		}
		this.setPagination(this.getWorkSummaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editWorkSummary")
	public String edit() {
		summaryTypes = this.getWorkSummaryTypeService().getAllWorkSummaryType();
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveWorkSummary", results = {
			@Result(name = "success", type = "redirect", location = "editWorkSummary"),
			@Result(name = "error", type = "chain", location = "editWorkSummary") })
	public String save() {
		if (workSummary.getTitle() == null
				|| "".equals(workSummary.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		if (workSummary.getType() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}
		if (workSummary.getReferPerson() == null) {
			addActionError("提交人不能为空");
			return ERROR;
		}
		if (workSummary.getAuditPerson() == null) {
			addActionError("审核人不能为空");
			return ERROR;
		}
		if (copyToPersons == null || "".equals(copyToPersons.trim())) {
			addActionError("抄送人不能为空");
			return ERROR;
		}
		this.setPrompt("提交工作总结成功");
		Person person = AppContext.getAccountContext().currentUser();
		workSummary.setReferPerson(person);
		workSummary.setState(Constrains.TASK_AUDITING);
		this.getWorkSummaryService().saveWorkSummary(workSummary, upload,
				uploadFileName, copyToPersons);
		return SUCCESS;
	}

	@Action("viewWorkSummaryCritique")
	public String viewWorkSummaryCritique() {
		return SUCCESS;
	}

	@Action("viewWorkSummryAudit")
	public String viewWorkSummryAudit() {
		return SUCCESS;
	}

	@Action("viewWorkSummary")
	public String view() {
		prepCopyToPersons();
		return SUCCESS;
	}

	@Action("critiqueWorkSummary")
	public String critiqueWorkSummary() {
		prepCopyToPersons();
		return SUCCESS;
	}

	private void prepCopyToPersons() {
		Iterator<Person> iter = workSummary.getCopyToPersons().iterator();
		while (iter.hasNext()) {
			copyToPersons = (copyToPersons == null) ? iter.next().getResume()
					.getName() : copyToPersons + " , "
					+ iter.next().getResume().getName();
		}
	}

	@Clew(results = { "success" })
	@Action(value = "submitCritiqueWorkSummary", results = { @Result(name = "success", type = "redirect", location = "listAuditWorkSummary") })
	public String submitCritiqueWorkSummary() {
		this.setPrompt("提交工作总结点评成功");

		workSummaryCritique.setWorkSummary(workSummary);
		this.getWorkSummaryCritiqueService().saveWorkSummaryCritique(
				workSummaryCritique, upload, uploadFileName);
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "passWorkSummary", results = { @Result(name = "success", type = "redirect", location = "listAuditWorkSummary") })
	public String passWorkSummary() {
		this.setPrompt("工作总结审核通过");
		workSummary.setState(Constrains.TASK_PASS);
		this.getWorkSummaryService().save(workSummary);

		workSummaryAudit.setWorkSummary(workSummary);
		workSummaryAudit.setState(Constrains.TASK_PASS);
		this.getWorkSummaryAuditService().saveWorkSummaryAudit(
				workSummaryAudit, upload, uploadFileName);
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "rebutWorkSummary", results = { @Result(name = "success", type = "redirect", location = "listAuditWorkSummary") })
	public String rebutWorkSummary() {
		this.setPrompt("工作总结审核打回");
		workSummary.setState(Constrains.TASK_NOTPASS);
		this.getWorkSummaryService().save(workSummary);

		workSummaryAudit.setWorkSummary(workSummary);
		workSummaryAudit.setState(Constrains.TASK_NOTPASS);
		this.getWorkSummaryAuditService().saveWorkSummaryAudit(
				workSummaryAudit, upload, uploadFileName);
		return SUCCESS;
	}

	@Action(value = "removeWorkSummary", results = {
			@Result(name = "success", type = "redirect", location = "listWorkSummary"),
			@Result(name = "error", type = "chain", location = "listWorkSummary") })
	public String remove() {
		if (ids == null) {
			this.addActionError("请选择要删除的工作任务");
			return ERROR;
		}

		this.getWorkSummaryService().batchRemove(ids);
		return SUCCESS;
	}

	public WorkSummary getWorkSummary() {
		return workSummary;
	}

	public void setWorkSummary(WorkSummary workSummary) {
		this.workSummary = workSummary;
	}

	public WorkSummaryService getWorkSummaryService() {
		return workSummaryService;
	}

	public void setWorkSummaryService(WorkSummaryService workSummaryService) {
		this.workSummaryService = workSummaryService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setSummaryTypes(List<WorkSummaryType> summaryTypes) {
		this.summaryTypes = summaryTypes;
	}

	public List<WorkSummaryType> getSummaryTypes() {
		return summaryTypes;
	}

	public void setWorkSummaryTypeService(
			WorkSummaryTypeService workSummaryTypeService) {
		this.workSummaryTypeService = workSummaryTypeService;
	}

	public WorkSummaryTypeService getWorkSummaryTypeService() {
		return workSummaryTypeService;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setCopyToPersons(String copyToPersons) {
		this.copyToPersons = copyToPersons;
	}

	public String getCopyToPersons() {
		return copyToPersons;
	}

	public WorkSummaryAudit getWorkSummaryAudit() {
		return workSummaryAudit;
	}

	public void setWorkSummaryAudit(WorkSummaryAudit workSummaryAudit) {
		this.workSummaryAudit = workSummaryAudit;
	}

	public WorkSummaryAuditService getWorkSummaryAuditService() {
		return workSummaryAuditService;
	}

	public void setWorkSummaryAuditService(
			WorkSummaryAuditService workSummaryAuditService) {
		this.workSummaryAuditService = workSummaryAuditService;
	}

	public WorkSummaryCritique getWorkSummaryCritique() {
		return workSummaryCritique;
	}

	public void setWorkSummaryCritique(WorkSummaryCritique workSummaryCritique) {
		this.workSummaryCritique = workSummaryCritique;
	}

	public WorkSummaryCritiqueService getWorkSummaryCritiqueService() {
		return workSummaryCritiqueService;
	}

	public void setWorkSummaryCritiqueService(
			WorkSummaryCritiqueService workSummaryCritiqueService) {
		this.workSummaryCritiqueService = workSummaryCritiqueService;
	}

	public String getYdmc() {
		return ydmc;
	}

	public void setYdmc(String ydmc) {
		this.ydmc = ydmc;
	}

}
